import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:banquanjie_app/app/services/HttpsRequest.dart';
class WorkListController extends GetxController {
  //TODO: Implement WorkListController
  final HttpsClient httpsClient = HttpsClient();
  final RxMap query = {
    "sortType": 1, //1:最新 2:最热
    "worksTypeFirst": '',
    "pageSize": 10,
    "pageCode": 1
  }.obs;
  final RxMap searchList = {}.obs;
  late RxList worksTypeFirstList = [].obs;
  final RxList workList = [].obs;
  final ScrollController scrollController = ScrollController();
  bool flag = false;
  final RxInt total = RxInt(-1);
  

  @override
  void onInit() {
    super.onInit();
    // fnGetSearchList();
    getQueryList();
    fnGet();
    scrollController.addListener(() {
      if (scrollController.position.pixels > scrollController.position.maxScrollExtent - 100) {
        if (flag == false && total.value > workList.length) {
          query['pageCode'] += 1;
          flag = true;
          fnGet();
        }
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.dispose();
  }

  void fnGetSearchList() async {
    var res = await httpsClient.get('/user/dic/ipListSearch');
    var result = res.data;
    if (result['code'] == 200) {
      searchList.value = result['data'];
      update();
    }
     
  }

  void getQueryList() async {
    var res = await httpsClient.post('/user/dic/list',  { "dicType": 'user_works_type', "parentId": 0 });
    var result = res.data;
    if (result['code'] == 200) {
      worksTypeFirstList.value = result['data'].map((item) => ({ "id": item['id'], "dicName": item['dicName']})).toList();
      worksTypeFirstList.insert(0, { "id": '', "dicName": '全部'});
      update();
    }
  }

  setSearchQueryValue(key, value) {
    if (key != 'sortType' && query[key] == value) {
      query[key] = '';
    } else {
      query[key] = value;
    }
    workList.value = [];
    update();
    fnGet();
  }


  void fnGet() async{
    var res = await httpsClient.post('/works/works/ip/list', query as Map<String, dynamic>);
     var result = res.data;
    if (result['code'] == 200) {
      workList.addAll(result['data']['rows']);
      total.value = result['data']['total'];
      flag = false;
      update();
    }
  }

  Future<void> onRefresh() async{
    workList.value = [];
    query['pageCode'] = 1;
    update();
    fnGet();
  }

 

}
